<?php

namespace App\Store\Controllers\Sysset\Commission;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Sysset\Commission\Changegoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Commission\Changegoods as ValidateChangegoods;

/**
 * 换货
 */
class ChangegoodsController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'change_type', 'change_channel', 'discount_min', 'discount_max', 'commission_rate', 'commission_rate_1', 'commission_rate_2']);
        $Validate = new ValidateChangegoods('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $store_ids = $req_data['store_ids'];
        $store_ids = is_array($store_ids) ? $store_ids : (is_array(json_decode($store_ids, true)) ? json_decode($store_ids, true) : []);

        foreach ($store_ids as $store_id) {
            $changegoods_data = [
                'cp_id' => $cp_id,
                'store_id' => $store_id,
                'change_type' => $req_data['change_type'],
                'change_channel' => $req_data['change_channel'],
                'discount_min' => !empty($req_data['discount_min']) ? $req_data['discount_min'] : 0,
                'discount_max' => !empty($req_data['discount_max']) ? $req_data['discount_max'] : 0,
                'commission_rate' => !empty($req_data['commission_rate']) ? $req_data['commission_rate'] : 0,
                'commission_rate_1' => !empty($req_data['commission_rate_1']) ? $req_data['commission_rate_1'] : 0,
                'commission_rate_2' => !empty($req_data['commission_rate_2']) ? $req_data['commission_rate_2'] : 0,
                'create_account_id' => $accountinfo['id'],
                'create_account_real_name' => $accountinfo['real_name'],
                'create_at' => time(),
            ];
            Changegoods::create($changegoods_data);
        }
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Changegoods::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Changegoods::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'store_id', 'change_type', 'change_channel', 'discount_min', 'discount_max', 'commission_rate', 'commission_rate_1', 'commission_rate_2']);
        $Validate = new ValidateChangegoods('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $req_data['discount_min'] = !empty($req_data['discount_min']) ? $req_data['discount_min'] : 0;
        $req_data['discount_max'] = !empty($req_data['discount_max']) ? $req_data['discount_max'] : 0;
        $req_data['commission_rate'] = !empty($req_data['commission_rate']) ? $req_data['commission_rate'] : 0;
        $req_data['commission_rate_1'] = !empty($req_data['commission_rate_1']) ? $req_data['commission_rate_1'] : 0;
        $req_data['commission_rate_2'] = !empty($req_data['commission_rate_2']) ? $req_data['commission_rate_2'] : 0;

        Changegoods::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateChangegoods('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Changegoods::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
